<script setup lang="ts">

import AudioPlayer from "~/components/video/AudioPlayer.vue";
import Seed from "~/components/ai/Seed.vue";
import LabelBase from "~/components/ai/LabelBase.vue";
import Prompt from "~/components/ai/PromptBase.vue";
import type {VoiceModel} from "~/utils/jiangying";
import {useUtilsUploadFileToOSS} from "~/utils/FileUtils";

const {voiceModels} = storeToRefs(useWorkEditStore())

// 添加ChatTTS声音模型
const audioSrc = ref('')
const testText = ref('人工智能时代，超级个体的时代即将来临，你准备好了吗？而我们将给你成为超级个体提供核心技术支撑，让AI真正成为生产力。如果真的有一天，AI变得非常成熟和简单，那还有你什么事？')
const voiceSeed = ref(0)
const voiceName = ref('')

const displayStyle = 'default'
const handleTestChatTTS = async () => {
  useSnackbarStore().showSuccessMessage('请求成功，正在进行声音合成')
  const output= await useVoiceTTS({text: testText.value, voice: voiceSeed.value + ''}, {ttsChannel: 'ChatTTS'})
  if (output){
    audioSrc.value=output
  }
}
/**
 * 保存当前的声音模型到voicemodels中
 */
const saveVoiceModel = () => {
  //voiceName不能为空
  if (!voiceName.value) {
    useSnackbarStore().showWarningMessage('声音名称不能为空')
    return false
  }
  const _chattts = {
    id: voiceModels.value.length,
    name: voiceName.value,
    voice: voiceSeed.value + '',
  } as VoiceModel
  voiceModels.value.push(_chattts)
  useSnackbarStore().showSuccessMessage('保存成功')
  return true
}
</script>

<template>
  <v-dialog max-width="600">
    <template v-slot:activator="{ props: activatorProps }">
      <v-btn
          prepend-icon="mdi-plus"
          v-bind="activatorProps"
          color="warning" class="ma-2">
        <span>增加ChatTTS角色</span>
      </v-btn>
    </template>
    <template v-slot:default="{ isActive }">
      <v-card>
        <v-card-title>
          <v-label class="text-h6 text-gray-600">
            添加ChatTTS声音角色
          </v-label>
        </v-card-title>
        <v-card-text>
          <Seed :length="4" label="随机声音种子" v-model="voiceSeed"/>
          <!--          测试文本-->
          <label-base>测试文本</label-base>
          <Prompt v-model="testText"></Prompt>
          <v-btn
              block
              class="ma-2"
              color="primary"
              variant="flat"
              prepend-icon="mdi-pencil"
              @click="handleTestChatTTS"
          >生成测试音频
          </v-btn>
          <label-base>声音试听</label-base>
          <audio-player :src="audioSrc" :displayStyle="displayStyle"></audio-player>
          <v-spacer/>
          <label-base>声音别名</label-base>
          <a-input v-model:value="voiceName"></a-input>
        </v-card-text>
        <v-card-actions>
          <v-spacer/>
          <v-btn color="primary" variant="text" @click="isActive.value=false">关闭</v-btn>
          <v-btn color="primary" variant="flat" @click="saveVoiceModel">保存</v-btn>

        </v-card-actions>
      </v-card>

    </template>


  </v-dialog>


</template>

<style scoped lang="scss">

</style>